package com.hyd.issues.user;

import com.hyd.dao.DataAdapter;
import com.hyd.dao.Table;
import com.hyd.issues.DaoUtil;

import java.util.List;
import java.util.Map;

/**
 * (description)
 *
 * @author yiding.he
 */
@Table(name = "users")
public class User {

    public static final String SESSION_KEY = "user";

    private static DataAdapter<User> adapter = DaoUtil.getDAO().createDataAdapter(User.class);

    //////////////////////////////////////////////////////

    private String username;

    private String nickname;

    private String email;

    private String password;

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    //////////////////////////////////////////////////////

    public static boolean checkLogin(String username, String password) {
        String checkUserPass = "select * from users where username=? and password=?";

        List<Map<String, Object>> users = DaoUtil.getDAO().query(checkUserPass, username, password);

        return users.size() > 0;
    }

    public static User getUser(String username) {
        return adapter.findBy("username", username);
    }

    public static boolean existsByName(String username) {
        return adapter.findBy("username", username) != null;
    }

    public static void saveUser(String username, String password) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        saveUser(user);
    }

    public static void saveUser(User user) {
        adapter.save(user);
    }
}
